// pages/expDetails/expDetails.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    like: true,
    noLike: "../../images/index/likebtnoff.png",
    isLike: "../../images/index/likebtnon.png",
    likePic: '', //点赞的图片
    collect: true,
    isCollect: "../../images/index/collecton.png",
    noCollect: "../../images/index/collectoff.png",
    collectPic: '', //收藏的图片
    result: "", //查询面经后的结果集
    isThanks: '', //是否点赞
    isCollections: '', //是否收藏
    showBottom: true, //显示底部
    showInput: false, //显示发布输入框
    comValue: '',
    getInput: ''
  },
  // 我有话说
  writeComment: function() {
    let that = this;
    that.setData({
      showBottom: false,
      showInput: true
    })
  },
  // 关闭发布
  closeInput: function() {
    this.setData({
      showBottom: true,
      showInput: false
    })
  },
  // 发布输入框
  getInput: function(e) {
    this.setData({
      comValue: e.detail.value
    })
  },
  // 发布
  sendComment: function() {
    let getComment = this.data.comValue;
    // 发布成功
    this.setData({
      showBottom: true,
      showInput: false
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewComment = {}
    offerInterviewComment.commentatorId = userId
    offerInterviewComment.commentatorName = userName
    offerInterviewComment.beCommentatorId = this.data.result.interviewExperience.tuser.id
      offerInterviewComment.beCommentatorName = this.data.result.interviewExperience.tuser.nickName
    offerInterviewComment.offerInterviewId = this.data.result.interviewExperience.interviewExperience.id
    offerInterviewComment.comments = getComment
    offerInterviewComment.commentType = 2 //1offer2面经
    util.request(api.OfferIntevieCommentAdd, offerInterviewComment, 'POST', true).then(function (res) {
      console.log(res)
    })
  },
  // 点赞
  cancelThaks: function(e) {
    let that = this;
    let isThanks = this.data.isThanks;
    if (isThanks) {
      that.setData({
        likePic: that.data.noLike
      })
    } else {
      that.setData({
        likePic: that.data.isLike
      })
    }
    this.setData({
      isThanks: !isThanks
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewThanksCollection = {}
    offerInterviewThanksCollection.collectionType = 2
    offerInterviewThanksCollection.offerInterviewId = this.data.result.interviewExperience.interviewExperience.id
    offerInterviewThanksCollection.thanksCollectionType = 1
    offerInterviewThanksCollection.userId = userId
    offerInterviewThanksCollection.userName = userName
    if (isThanks) {
      util.request(api.ThanksCollectionCancle, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    } else {
      util.request(api.ThanksCollectionAdd, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    }
  },
  // 收藏
  cancelCollect: function(e) {
    let that = this;
    let isCollections = this.data.isCollections;
    if (isCollections) {
      that.setData({
        collectPic: that.data.noCollect
      })
    } else {
      that.setData({
        collectPic: that.data.isCollect
      })
    }
    this.setData({
      isCollections: !isCollections
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewThanksCollection = {}
    offerInterviewThanksCollection.collectionType = 2
    offerInterviewThanksCollection.offerInterviewId = this.data.result.interviewExperience.interviewExperience.id
    offerInterviewThanksCollection.thanksCollectionType = 2
    offerInterviewThanksCollection.userId = userId
    offerInterviewThanksCollection.userName = userName
    if (isCollections) {
      util.request(api.ThanksCollectionCancle, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    } else {
      util.request(api.ThanksCollectionAdd, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    //参数携带完毕，开始发起请求查询面经
    // util.request(api.InterviewDetail + "?userId=" + userId + "&interviewId=" + options.interviewId, null, 'GET', true).then(function(res) {
    util.request(api.InterviewDetail + "?userId=" + userId + "&interviewId=" + options.offerInterviewId, null, 'GET', true).then(function(res) {
      console.log(res)
      if (res.code === 200) {
        if (res.result.isThanks) {
          that.setData({
            likePic: that.data.isLike
          })
        } else {
          that.setData({
            likePic: that.data.noLike
          })
        }
        if (res.result.isCollections) {
          that.setData({
            collectPic: that.data.isCollect
          })
        } else {
          that.setData({
            collectPic: that.data.noCollect
          })
        }
        that.setData({
          result: res.result,
          isThanks: res.result.isThanks,
          isCollections: res.result.isCollections
        })
      }
    });
    console.log(that.data.result);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})